package com.iweb.dao;

import com.iweb.db.XdDataSource;
import com.iweb.pojo.User;
import com.iweb.result.Result;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;

@Slf4j
public class UserDao {

    private PreparedStatement pst;
    private ResultSet rs;

    // 根据手机号查询用户
    public User selectUserByPhone(String phone) {
        Connection conn = XdDataSource.getConnection();
        User user = null;
        String sql = "select * from xd_user where phone = ?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, phone);
            rs = pst.executeQuery();
            while (rs.next()) {
                user = createUser(rs);
            }
        } catch (Exception e) {
            log.info(e.getMessage());
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return user;
    }

    // 保存用户
    public void save(User user) {
        Connection conn = XdDataSource.getConnection();
        String sql = "insert into xd_user (id,phone,password,create_time) values (?,?,?,?)";

        try {
            pst = conn.prepareStatement(sql);
            // 设定参数
            pst.setString(1, user.getId());
            pst.setString(2, user.getPhone());
            pst.setString(3, user.getPassword());
            pst.setDate(4, new Date(user.getCreateTime().getTime()));
            // 执行插入
            pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 从结果集中封装user对象
    private User createUser(ResultSet rs) throws Exception {
        User user = new User();
        user.setId(rs.getString("id"));
        user.setNickname(rs.getString("nickname"));
        user.setAvatar(rs.getString("avatar"));
        user.setPhone(rs.getString("phone"));
        user.setEmail(rs.getString("email"));
        user.setPassword(rs.getString("password"));
        user.setName(rs.getString("name"));
        user.setBirth(rs.getDate("birth"));
        user.setCreateTime(rs.getDate("create_time"));
        user.setAddr(rs.getString("addr"));
        return user;
    }

    public User selectUserByPhoneOrEmail(User user) {
        User userData = null;
        String sql = "select * from xd_user where phone = ? or email = ?";
        Connection connection = XdDataSource.getConnection();
        try {
            pst = connection.prepareStatement(sql);
            pst.setString(1, user.getPhone());
            pst.setString(2, user.getEmail());

            rs = pst.executeQuery();
            while (rs.next()) {
                userData = createUser(rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return userData;
    }

    // 更具用户id查询用户信息
    public User findById(String id) {
        Connection conn = XdDataSource.getConnection();
        User user = null;
        try {
            // TO DO
            String sql = "select * from xd_user where id = ?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, id);

            ResultSet rs = pst.executeQuery();

            while (rs.next()) {
                user = createUser(rs);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return user;
    }

    public void updateEmailById(String userId, String email) {
        Connection conn = XdDataSource.getConnection();
        try {
            // TO DO
            String sql = "update xd_user set email = ? where id = ?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, email);
            pst.setString(2, userId);
            pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }


    public void updatePasswordById(String userId, String password) {
        Connection conn = XdDataSource.getConnection();
        try {
            // TO DO
            String sql = "update xd_user set password = ? where id = ?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, password);
            pst.setString(2, userId);
            pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
